using UnityEngine;
using UnityEngine.UI;

public class ItemVehicleScript : MonoBehaviour
{
	public string keyName;

	public string CN_Name;

	public Sprite spAvatar;

	public Image imgAvatar;

	public int price;

	public Text txtName;

	public Text txtPrice;

	public Button btnUnlock;

	public Button btnSelect;

	public bool unlocked;

	private bool notVehicle;

	private int indexVehicle;

	public void SetInfo(ITEM_VEHICLE TEMP, int index)
	{
		spAvatar = TEMP.sprite;
		keyName = TEMP.name;
		price = TEMP.price;
		indexVehicle = index;
		CN_Name = TEMP.CN_name;
		_Start();
	}

	public void _Start()
	{
		if (keyName != "")
		{
			unlocked = ((PlayerPrefs.GetInt(keyName + "unlocked") != 0) ? true : false);
			//txtName.text = keyName;
			txtName.text = CN_Name;
			notVehicle = false;
		}
		else
		{
			txtName.text = "NONE";
			unlocked = true;
			notVehicle = true;
		}
		if ((bool)spAvatar)
		{
			imgAvatar.sprite = spAvatar;
		}
		else
		{
			imgAvatar.enabled = false;
		}
		if (!unlocked)
		{
			txtPrice.text = string.Concat(price);
			btnUnlock.gameObject.SetActive(value: true);
			btnUnlock.onClick.AddListener(delegate
			{
				ClickUnlock();
			});
		}
		else
		{
			btnUnlock.gameObject.SetActive(value: false);
		}
		btnSelect.onClick.AddListener(delegate
		{
			SelectVehicle();
		});
	}

	public void ClickUnlock()
	{
		if (CoinManager.CheckBuy(price))
		{
			CoinManager.coin -= price;
			PlayerPrefs.SetInt(keyName + "unlocked", 1);
			CoinManager.UpCoin();
			GUIManager.Instance.UpdateTextCoin();
			btnUnlock.gameObject.SetActive(value: false);
			DataManager.Instance.AddVehicle(1);
			//FirebaseStat.Instance.LogNormalEvent("ns_unlock_vehicle", "vehicle_id", (indexVehicle + 1).ToString());
		}
	}

	public void SelectVehicle()
	{
		if (!notVehicle)
		{
			VehiclesManager.Instance.ChangeVehicle(keyName);
		}
		else
		{
			PlayerControlManager.Instance.isVehicles = false;
			PlayerControlManager.Instance.click_Reset();
			VehiclesManager.Instance.indexVehicles = -1;
		}
		GUIManager.Instance.HideSelectVehicle();
	}

	public int CheckStatus()
	{
		int result = 0;
		if (!unlocked && CoinManager.CheckBuy(price))
		{
			result = 1;
		}
		return result;
	}
}
